package com.xmm.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class Library  implements Music {
	private String mCodeId = "Library" + 
			String.valueOf(System.currentTimeMillis()) +
			String.valueOf(Math.random());
	
	private String mName = null;
	private List<Music> mMusics = new ArrayList<Music>();
	private Map<String, Integer> mMusicIndexMap = new HashMap<String, Integer>();
	
	private int mRunningTime = 0;
	private int mFileSize = 0;
	private double mAverageRating = 0.0;
	
	public Library(String name) {
		mName = name;
	}
	
	public String getName() {
		return mName;
	}
	public void addMusic(Music music) {
		if(music == null) {
			return;
		}
		mMusicIndexMap.put(music.getCodeId(), mMusics.size());
		mMusics.add(music);
	}
	private Music removeMusic(int index) {
		if(index < 0  || index >= mMusics.size()) {
			return null;
		}
		
		return mMusics.remove(index);
	}
	
	public Music removeFrom(String codeId) {
		if(codeId == null) return null;
		Integer iIndex = mMusicIndexMap.remove(codeId);
		if(iIndex == null) return null;
		int index = iIndex.intValue();
		return removeMusic(index);
	}
	
	public Music getMusic(int index) {
		if(index < 0  || index >= mMusics.size()) {
			return null;
		}
		
		return mMusics.get(index);
	}
	
	public List<Track> createRandTrackListWithMinRating() {
		return null;
	}

	@Override
	public String getCodeId() {
		return mCodeId;
	}
	
	@Override
	public int getClidrenCount() {
		if(mMusics == null) return 0;
		return mMusics.size();
	}

	@Override
	public double getAveRating() {
		// TODO Auto-generated method stub
		return mAverageRating;
	}

	@Override
	public long getLength() {
		// TODO Auto-generated method stub
		return mRunningTime;
	}

	@Override
	public long getSize() {
		// TODO Auto-generated method stub
		return mFileSize;
	}
 }
